package com.itcms.core.service.impl;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itcms.common.util.ReadConfig;
import com.itcms.core.domain.Role;
import com.itcms.core.domain.User;
import com.itcms.core.domain.UserTodoTotal;
import com.itcms.core.repository.UserTodoTotalDao;
import com.itcms.core.service.RoleService;
import com.itcms.core.service.UserService;
import com.itcms.core.service.UserTodoTotalService;
import com.itcms.core.support.Context;
import com.itcms.webservice.util.WSClientUtil;
import com.itcms.webservice.uuv.todoCount.TodoCountServiceSoap;
import com.itcms.webservice.uuv.todoCount.SendTodoCountResponse.SendTodoCountResult;

/**
 * 
 * @author mengjie
 * 
 */

@Service
@Transactional(readOnly = true)
public class UserTodoTotalServiceImpl implements UserTodoTotalService{
	public static final int[] ROLE_ID = {8,9,10};
	public List<UserTodoTotal> findList(){
		return dao.findList();
	}

	public void sendTodo1() {
		//往oa推送待办
		Integer siteId = Context.getCurrentSiteId();
		List<Role> roles = roleService.findList(siteId);
		for (Role role : roles) {
//			if(ArrayUtils.contains(ROLE_ID, role.getId())){
			if(role.getId() != 12){//角色不为文档管理
				Set<User> users = role.getUsers();
				for (User user : users) {
					String loginID = user.getUsername();
					String userCode = user.getUserCode();
					System.out.println("角色用户--"+loginID+" : "+userCode);
				}
			}
		}
		
		List<UserTodoTotal> userTodoTotals = findList();
		for (UserTodoTotal userTodoTotal : userTodoTotals) {
			System.out.println(userTodoTotal.getUsername()+" : "+userTodoTotal.getTotal());
		}
	}
	
	public void sendTodo() {
		Object o = WSClientUtil.uuvSoapFactoryBean(ReadConfig.getString("uuv_todoCount"),TodoCountServiceSoap.class);
		TodoCountServiceSoap todoCountServiceSoap = (TodoCountServiceSoap)o;
		List<UserTodoTotal> userTodoTotals = findList();
		
		Integer siteId = Context.getCurrentSiteId();
		String appID = ReadConfig.getString("appID");
		Integer total = 0;
		List<Role> roles = roleService.findList(siteId);
		for (Role role : roles) {
//			if(ArrayUtils.contains(ROLE_ID, role.getId())){
			if(role.getId() != 12){//角色不为文档管理
				Set<User> users = role.getUsers();
				for (User user : users) {
					String loginID = user.getUsername();
					String userCode = user.getUserCode();
					todoCountServiceSoap.sendTodoCount(appID, userCode, loginID, total);
				}
			}
		}
		/*List<User> users = service.findAll();
		for (User user : users) {
			String loginID = user.getUsername();
			String userCode = user.getUserCode();
			todoCountServiceSoap.sendTodoCount(appID, userCode, loginID, total);
		}*/
		System.out.println("all user's todo count init over!");
		for (UserTodoTotal userTodoTotal : userTodoTotals) {
			try {
				String loginID = userTodoTotal.getUsername();
				String userCode = userTodoTotal.getUserCode();
				total = userTodoTotal.getTotal();
				todoCountServiceSoap.sendTodoCount(appID, userCode, loginID, total);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Autowired
	private UserTodoTotalDao dao;
	@Autowired
	private UserService service;
	@Autowired
	private RoleService roleService;
}